﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Caliburn.Micro;
using PurchaseSales.Manage.Common;
using PurchaseSales.Manage.Services;
using ReactiveUI.Xaml;

namespace PurchaseSales.Manage.ViewModels
{
    public class ShellViewModel : Conductor<IScreen>.Collection.OneActive, IShell
    {
        private string _loginUser;
        public string LoginUser
        {
            get { return _loginUser; }
            set { _loginUser = value; NotifyOfPropertyChange(() => LoginUser); }
        }

        public BindableCollection<ModleApplication> ModleApplications { get; set; }
        public ReactiveCommand BackCommand { get; set; }
        public ShellViewModel()
        {
            // LoginUser = ApplicationCache.Get("Agent").ToString();
            InitialDatas();
            IsPageShow = false;
            State = "hidden";
            BackCommand = new ReactiveCommand();
            BackCommand.SubscribeOnDispatcher().Subscribe(o =>
                                                {
                                                    var listBox = o as ListBox;
                                                    if (listBox != null)
                                                        listBox.SelectedIndex = -1;
                                                    if (Items.Contains(_screen))
                                                        Items.Remove(_screen);
                                                    IsPageShow = false;
                                                    DisplayName = "Home";
                                                });
        }

        private void InitialDatas()
        {
            DisplayName = "Home";

            ModleApplications = new BindableCollection<ModleApplication>();

            var keys = ApplicationCache.Get(FunctionKeys.FunctionModules) as IList<string>;
            var allModules = FunctionModule.GetAllFunction();
            foreach (var allModule in allModules)
            {
                if (keys != null && keys.Contains(allModule.Key))
                {
                    ModleApplications.Add(new ModleApplication(allModule.Value, allModule.Key, "/Resources/Images/" + allModule.Key.Replace("ViewModel", ".png")));
                }

            }

        }

        private string _state;
        public string State
        {
            get { return _state; }
            set { _state = value; NotifyOfPropertyChange(() => State); }
        }

        private bool _isPageShow;
        public bool IsPageShow
        {
            get { return _isPageShow; }
            set { _isPageShow = value; NotifyOfPropertyChange(() => IsPageShow); }
        }

        private IScreen _screen;
        private readonly Assembly _assembly = Assembly.Load("PurchaseSales.Manage");

        public void ModelSelected(ModleApplication application)
        {
            if (application == null)
                return;
            var viewmodel = _assembly.GetType("PurchaseSales.Manage.ViewModels." + application.Key);
            _screen = IoC.GetInstance(viewmodel, application.Key) as IScreen;
            DisplayName = application.Name;
            Items.Add(_screen);
            ActivateItem(_screen);
            IsPageShow = true;
        }
    }
}
